package fr.generali.accueilclient.gwt.client.ui.components.menu.tab;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasMouseOutHandlers;
import com.google.gwt.event.dom.client.HasMouseOverHandlers;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

import fr.generali.accueilclient.gwt.client.ui.components.menu.Resources;

/**
 * Un menu est constitué d'un titre et d'un conteneur dans lequel on peut mettre
 * ce que l'on veut.<br>
 */
public class TabMenu extends PopupPanel implements IsWidget, HasWidgets, HasMouseOverHandlers, HasMouseOutHandlers {

    private static HeaderTabMenuUiBinder uiBinder = GWT.create(HeaderTabMenuUiBinder.class);

    interface HeaderTabMenuUiBinder extends UiBinder<Widget, TabMenu> {
    }

    @UiField
    Resources res;

    @UiField
    FlowPanel container;

    @UiField
    HTML menuTitle;

    @UiField
    FocusPanel closeMenu;

    public TabMenu() {
        setWidget(uiBinder.createAndBindUi(this));
        res.style().ensureInjected();
        res.layout().ensureInjected();
    }

    @Override
    public void setTitle(String title) {
        menuTitle.setHTML(title);
    }

    @Override
    public void setWidth(String width) {
        container.setWidth(width);
    }

    @Override
    public void add(Widget w) {
        container.add(w);
    }

    @Override
    public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
        return addDomHandler(handler, MouseOutEvent.getType());
    }

    public HandlerRegistration addCloseMenuClickHandler(ClickHandler handler) {
        return closeMenu.addClickHandler(handler);
    }

    @Override
    public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
        return addDomHandler(handler, MouseOverEvent.getType());
    }
}
